const lodash = require("lodash")
import Base from "../entities/Base"
import { dictToWhere } from '@/utils/sqlUtils'

const PromotionDao = {
    table: "promotion",
    pageSize: Base.pageSize
}

PromotionDao.initialForm = () => {
    return JSON.parse(JSON.stringify({
        id: 0,
        lang: "",
        name: "",
        template: "",
        keywords: "",
        platformId: "",
        sourceField: "",
        targetField: "",
        enable: 1,
    }))
}

PromotionDao.getFields = async () => {
    let sql = "SELECT DISTINCT targetField FROM " + PromotionDao.table
    return await Base.query(sql)
}

PromotionDao.fetchAll = async (dict) => {
    let whereSql = dictToWhere(dict)
    return await Base.fetchAll(PromotionDao.table, whereSql)
}

PromotionDao.fetchPage = async (dict, page) => {
    let whereSql = dictToWhere(dict)
    return await Base.fetchPage(PromotionDao.table, whereSql, page)
}

PromotionDao.fetchOne = async (dict) => {
    let whereSql = dictToWhere(dict)
    let objs = await Base.fetchOne(PromotionDao.table, whereSql)
    if (objs.length > 0) {
        return objs[0]
    }
    return null
}

PromotionDao.fetchCount = async (dict, page) => {
    let count = await PromotionDao.countWhere(dict)
    let datas = await PromotionDao.fetchPage(dict, page)
    return { count, datas }
}

PromotionDao.countWhere = async (dict) => {
    let whereSql = dictToWhere(dict)
    console.log("whereSql", whereSql)
    return await Base.countWhere(PromotionDao.table, whereSql)
}

PromotionDao.save = async (obj) => {
    return await Base.save(PromotionDao.table, obj)
}

PromotionDao.delete = async (id) => {
    return await Base.delete(PromotionDao.table, id)
}


export { PromotionDao }